package org.pps.gl.client.vue;

import org.pps.gl.client.LoginService;
import org.pps.gl.client.LoginServiceAsync;
import org.pps.gl.client.Pps_gl;
import org.pps.gl.client.events.ConnectedEvent;
import org.pps.gl.client.events.ConnectedEventHandler;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

/**
 * Classe représentant le cadre affiché pour la connexion, email et mot de
 * passe.
 * 
 */
public class HDR_Login extends Composite implements ConnectedEventHandler {
	private TextBox textBoxEmail;
	private TextBox textBoxPassword;
	private FlexTable flexTable;
	private Button btnConnexion;
	final DialogBox reglages = new DialogBox();
	
	public HDR_Login() {
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(5);
		verticalPanel.setStyleName("cadre");
		initWidget(verticalPanel);
		verticalPanel.setSize("363px", "107px");

		flexTable = new FlexTable();
		flexTable.setBorderWidth(0);
		verticalPanel.add(flexTable);
		flexTable.setSize("357px", "56px");

		Label lblEmail = new Label("Email");
		lblEmail.setStyleName("grayed");
		flexTable.setWidget(0, 0, lblEmail);
		lblEmail.setSize("93px", "21px");

		textBoxEmail = new TextBox();
		flexTable.setWidget(0, 1, textBoxEmail);
		textBoxEmail.setWidth("180px");

		Label lblPassword = new Label("Mot de passe");
		lblPassword.setStyleName("grayed");
		flexTable.setWidget(1, 0, lblPassword);
		lblPassword.setHeight("25px");

		textBoxPassword = new PasswordTextBox();

		flexTable.setWidget(1, 1, textBoxPassword);
		textBoxPassword.setWidth("180px");

		btnConnexion = new Button("Connexion");
		btnConnexion.setStyleName("boutonConnect");
		btnConnexion.setText("");
		btnConnexion.setStylePrimaryName("boutonConnect");

		flexTable.setWidget(1, 2, btnConnexion);
		btnConnexion.setSize("36px", "36px");

		// Gestion des évènements
		btnConnexion.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				seConnecter();
			}
		});

		Label hprlnkMotDePasse = new Label("Mot de passe oublié ?");
		hprlnkMotDePasse.setStylePrimaryName("footerHyper");

		// envoyer un email si perte du mot de passe
		hprlnkMotDePasse.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (textBoxEmail.getText().isEmpty())
					Window.alert("Veuillez saisir votre adresse email.");
				else {
				LoginServiceAsync loginService = GWT.create(LoginService.class);
				loginService.changerPassword(textBoxEmail.getText(),
						new AsyncCallback<Boolean>() {
							public void onFailure(Throwable error) {
								Window.alert("Vous n'êtes pas encore inscrit sur notre site.");
							}

							public void onSuccess(Boolean result) {
								if (result)
									Window.alert("Vous venez de recevoir un mail avec un nouveau mot de passe.");
								else
									Window.alert("Vous n'êtes pas encore inscrit sur notre site.");
							}
						});
				}
			}
		});

		flexTable.setWidget(2, 1, hprlnkMotDePasse);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1,
				HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setVerticalAlignment(1, 1,
				HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 1,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(1, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 2,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(1, 2,
				HasVerticalAlignment.ALIGN_MIDDLE);

		textBoxPassword.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER)
					seConnecter();
			}
		});

		textBoxEmail.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER)
					seConnecter();
			}
		});
	}

	public void seConnecter() {
		if (textBoxEmail.getText().length() == 0
				|| textBoxPassword.getText().length() == 0) {
			Window.alert("Email ou mot de passe manquant.");
		} else {
			Pps_gl.getCm().connect(textBoxEmail.getText(),
					textBoxPassword.getText());
			
			Timer timer = new Timer(){ 
				public void run(){ 
					btnConnexion.setEnabled(false);
				}
			};
			timer.schedule(30);
			btnConnexion.setEnabled(true);
			
		}
	}

	@Override
	public void onConnected(ConnectedEvent connectedEvent) {
		btnConnexion.setEnabled(true);
	}
	
}
